class Solution {
public:
    int minCost(vector<vector<int>>& costs) {
        int m = costs.size();

        vector<vector<int>> dp(m + 1, vector<int>(3, INT_MAX));

        dp[0][0] = dp[0][1] = dp[0][2] = 0;
        for (int i = 1; i <= m; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                switch (j)
                {
                case 0:
                    dp[i][j] = min(dp[i - 1][1], dp[i - 1][2]);
                    break;
                case 1:
                    dp[i][j] = min(dp[i - 1][0], dp[i - 1][2]);
                    break;
                case 2:
                    dp[i][j] = min(dp[i - 1][0], dp[i - 1][1]);
                }
                dp[i][j] += costs[i - 1][j];
            }
        }

        return min({ dp[m][0], dp[m][1], dp[m][2] });

    }
};